﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
using EmbeddedSharp.Core;
using WeifenLuo.WinFormsUI.Docking;
using Zove.Runtime;

namespace Zove.Desktop {
    public partial class PlotExplorerView : DockContent {
        public PlotExplorerView() {
            InitializeComponent();
        }

        private void btnPlot_Click(object sender, EventArgs e) {
            double from = Convert.ToDouble(this.tbFrom.Text);
            double to = Convert.ToDouble(this.tbTo.Text);

            string variable = this.tbVariable.Text;
            string expr = this.txtExpression.Text.Replace(variable, "x");
            Parser p = DefaultDependencyManager.Instance.ParserFactory.CreateParser(expr);
            Node node = p.Parse();

            CSharpExpressionTranslator translator
                = new CSharpExpressionTranslator();
            string csharpExpr = translator.Translate(node);
            CSharpFunctionCompiler csharpfc = new CSharpFunctionCompiler();
            IUnaryParameterFunction func = csharpfc.Parse(csharpExpr);

            GraphPane myPane = zg1.GraphPane;

            // Set the titles and axis labels
            myPane.Title.Text = "My Test Graph";
            myPane.XAxis.Title.Text = "X Value";
            myPane.YAxis.Title.Text = "My Y Axis";

            // Make up some data points from the Sine function
            PointPairList list = new PointPairList();
            for (double x = from; x < to; x=x+(to-from)/100) {
                double y = func.Eval(x);

                list.Add(x, y);
            }

            // Generate a blue curve with circle symbols, and "My Curve 2" in the legend
            LineItem myCurve = myPane.AddCurve("My Curve", list, Color.Blue,
                                    SymbolType.None);
            // Fill the area under the curve with a white-red gradient at 45 degrees
            //myCurve.Line.Fill = new Fill(Color.White, Color.Red, 45F);
            // Make the symbols opaque by filling them with white
            //myCurve.Symbol.Fill = new Fill(Color.White);

            // Fill the axis background with a color gradient
            myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45F);

            // Fill the pane background with a color gradient
            myPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45F);

            // Calculate the Axis Scale Ranges
            this.zg1.AxisChange();
        }
    }
}
